package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.MenuDao;
import com.aaa.book_mall.entity.Menu;
import com.aaa.book_mall.util.JDBCUtil;
import com.aaa.book_mall.util.Tree;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuDaoImpl
 * @Description ToDo
 * @Auther teacher Bai
 * @Date 2021/1/19 9:35
 */
@Repository
public class MenuDaoImpl implements MenuDao {

    @Override
    public List<Menu> getAllMenuByRid(Integer rid) {
        String sql = "SELECT tm.mid,tm.mname,tm.mcode,tm.murl,tm.mpid,tm.micon from t_role_premission trp INNER JOIN t_menu tm\n" +
                "ON trp.mid=tm.mid \n" +
                "where tm.mpid = 0 and trp.rid=?";
        //1.查询出List<Map>数据
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, rid);
        //将数据转储到List<Menu>数据结构中
        List<Menu> menus = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            //此时还没有查子菜单，所以先为null
            Menu menu = new Menu((Integer) map.get("mid"),(String)map.get("mname"),(String)map.get("murl"),null);
            //2.根据父菜单的mid查询对应的子菜单信息
            String sonSql = "SELECT tm.mid,tm.mname,tm.mcode,tm.murl,tm.mpid,tm.micon from t_role_premission trp INNER JOIN t_menu tm\n" +
                    "ON trp.mid=tm.mid \n" +
                    "where trp.rid=? and tm.mpid=?";
            //此处就是根据父的mid和rid查到的所有的子菜单信息
            List<Map<String, Object>> sonMaps = JDBCUtil.queryListMap(sonSql, rid, map.get("mid"));
            //将所有的子菜单信息也转储到List<Menu>
            ArrayList<Menu> sonMenus = new ArrayList<>();
            for (Map<String, Object> sonMap : sonMaps) {
                Menu sonMenu = new Menu((Integer) sonMap.get("mid"), (String) sonMap.get("mname"), (String) sonMap.get("murl"));
                sonMenus.add(sonMenu);
            }
            menu.setSonMenu(sonMenus);
            menus.add(menu);
        }
        System.out.println(menus);
        return menus;
    }

    @Override
    public List<Tree> getAllMenu() {
        String sql = "SELECT mid,mname from t_menu tm where tm.mpid=0";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql);
        List<Tree> trees = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Tree tree = new Tree((Integer) map.get("mid"), (String) map.get("mname"), null);
            //查询子菜单
            String sonSql = "SELECT mid,mname from t_menu tm where tm.mpid=?";
            List<Map<String, Object>> sonMaps = JDBCUtil.queryListMap(sonSql, map.get("mid"));
            //创建一个List<Tree>用来装所有的子菜单信息
            List<Tree> sonTrees = new ArrayList<>();
            for (Map<String, Object> sonMap : sonMaps) {
                Tree sonTree = new Tree((Integer) sonMap.get("mid"), (String) sonMap.get("mname"), null);
                sonTrees.add(sonTree);
            }
            tree.setChildren(sonTrees);
            trees.add(tree);
        }
        return trees;
    }

    @Override
    public List getAllMidByRid(Integer rid) {
        String sql = "SELECT trp.mid from t_role_premission trp INNER JOIN t_menu tm\n" +
                "ON tm.mid=trp.mid\n" +
                "where rid=? and tm.mpid!=0";
        List<List> lists = JDBCUtil.queryList(sql,rid);
        ArrayList<Object> list1 = new ArrayList<>();
        if(lists.size()>0){
            for (List list : lists) {
                Object o = list.get(0);
                list1.add(o);
            }
            return list1;
        }
        return null;
    }

    @Override
    public int updatePre(Integer rid, String[] midArr) {
        //1.先将角色和权限菜单的中间表中所有当前rid对应的数据删除掉，
        String sql = "delete from t_role_premission where rid=?";
        JDBCUtil.update(sql,rid);
        //2.在根据前台传来的数据重新添加int
        int i=0;
        for (String mid : midArr) {
            String sql1 = "insert into t_role_premission values(null,?,?)";
            i = JDBCUtil.update(sql1, rid, mid);
        }
        return i;
    }
}
